package ink.xiaobaibai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ink.xiaobaibai.common.PageInfo;
import ink.xiaobaibai.dao.OxygenDeviceDao;
import ink.xiaobaibai.entity.OxygenDevice;
import ink.xiaobaibai.entity.OzoneDevice;
import ink.xiaobaibai.enumOxy.DeviceStatus;
import ink.xiaobaibai.service.IAdminUserService;
import ink.xiaobaibai.service.IOxygenDeviceService;
import ink.xiaobaibai.service.IOzoneDeviceService;
import ink.xiaobaibai.vo.UserByDeviceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiaobaibai
 * @since 2021-05-22
 */
@Service
public class OxygenDeviceServiceImpl extends ServiceImpl<OxygenDeviceDao, OxygenDevice> implements IOxygenDeviceService {

    @Autowired
    private IAdminUserService adminUserService;

    @Autowired
    private IOzoneDeviceService ozoneDeviceService;

    @Override
    public PageInfo<OxygenDevice> getOxygenDeviceList(Integer pageNum, Integer pageSize, Integer userId, Integer depId, String provinceCity, String status) {
        List<Integer> ids = null;
        if (depId != null) {
            ids = this.adminUserService.getUserIdByDepId(depId);
        } else if (userId != null) {
            ids = Collections.singletonList(userId);
        }

        int limitStart = (pageNum - 1) * pageSize;
        int limitEnd = pageSize;
        List<OxygenDevice> list = this.baseMapper.getOxygenDeviceList(limitStart, limitEnd, ids, status, provinceCity != null ? provinceCity + "%" : null);
        Integer count = this.baseMapper.getOxygenDeviceCount(ids, status, provinceCity != null ? provinceCity + "%" : null);
        return new PageInfo<>(list, count, pageSize);
    }

    @Override
    public UserByDeviceVo getUserInfoByUserId(Integer userId) {
        return this.baseMapper.getUserInfoByUserId(userId);
    }

    @Override
    public void updateDeviceStatus(Integer deviceId, DeviceStatus status) {
        //故障,close所有臭氧机,两机该状态
        QueryWrapper<OzoneDevice> q1 = new QueryWrapper<>();
        q1.eq("oxygen_id", deviceId);
        List<OzoneDevice> list = this.ozoneDeviceService.list(q1);
        //将所有臭氧机都改为停运状态
        /**
         * 臭氧延时队列出来改变状态时,应该判断当前状态是否为工作中,如果不是,就不改变
         * 因为此时可能已经故障或者停运了 延时队列取出时已经校验,所以无需担心
         */
        List<Integer> ids = list.stream().map(OzoneDevice::getId).collect(Collectors.toList());
        UpdateWrapper<OzoneDevice> u1 = new UpdateWrapper<>();
        u1.in("id", ids);
        u1.set("status", status.getStatus());
        if (ids.size() != 0) {
            this.ozoneDeviceService.update(u1);
        }
        //将氧气机改为停运状态
        UpdateWrapper<OxygenDevice> u2 = new UpdateWrapper<>();
        u2.set("status", status.getStatus());
        u2.eq("id", deviceId);
        this.update(u2);
    }

}
